package com.kgc.crowd.distributioncrowdcommon.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class UploadUtil {
    public static void uploadSingleFile(
            String endpoint,
            String accessKeyId,
            String accessKeySecret,
            String fileName,
            String folderName,
            String bucketName,
            InputStream inputStream){
        try{
            // 创建OSSClient实例。
            OSS ossClient=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
            // 存入对象的名称=目录名称+"/"+文件名
            String objectName=folderName+"/"+fileName;
            ossClient.putObject(bucketName,objectName,inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();

        }catch (OSSException e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
    /**
     * 生成文件名
     * @param originalFileName 原始文件名
     * @return
     */
    public static String generateFileName(String originalFileName){
        return UUID.randomUUID().toString().replaceAll("-","")+originalFileName;
    }
    /**
     * 根据日期生成目录名称
     * @return
     */
    public static String generateDayFolderName(){
        return new SimpleDateFormat("yyyyMMdd").format(new Date());
    }

}
